package com.ruoyi.web.controller.module.examine;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.system.domain.module.examine.ExamineApproveRegAdminRecord;
import com.ruoyi.system.domain.module.wx.WxUser;
import com.ruoyi.system.service.module.examine.IExamineApproveRegAdminRecordService;
import com.ruoyi.system.service.module.wx.IWxUserService;
import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.web.controller.common.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 审批流程记录的 审核人员处理记录Controller
 *
 * @author ruoyi
 * @date 2025-04-07
 */
@Validated
@Api(value = "审批流程记录的 审核人员处理记录控制器", tags = {"审批流程记录的 审核人员处理记录管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/module/adminRecord")
public class ExamineApproveRegAdminRecordController extends BaseController {

    private final IExamineApproveRegAdminRecordService examineApproveRegAdminRecordService;
	private final IWxUserService wxUserService;


    /**
     * 查询审批流程记录的 审核人员处理记录列表
     */
    @ApiOperation("查询审批流程记录的 审核人员处理记录列表")
    @PreAuthorize("@ss.hasPermi('module:adminRecord:list')")
    @GetMapping("/list")
    public TableDataInfo<ExamineApproveRegAdminRecord> list(@Validated ExamineApproveRegAdminRecord examineApproveRegAdminRecord) {
        QueryWrapper<ExamineApproveRegAdminRecord> qw = new QueryWrapper<>();

        TableDataInfo<ExamineApproveRegAdminRecord> td = PageUtils.buildDataInfo(examineApproveRegAdminRecordService.page(PageUtils.buildPage(),qw));
        List<ExamineApproveRegAdminRecord> list = td.getRows();
        return td;
    }

    /**
     * 导出审批流程记录的 审核人员处理记录列表
     */
    @ApiOperation("导出审批流程记录的 审核人员处理记录列表")
    @PreAuthorize("@ss.hasPermi('module:adminRecord:export')")
    @Log(title = "审批流程记录的 审核人员处理记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<ExamineApproveRegAdminRecord> export(@Validated ExamineApproveRegAdminRecord examineApproveRegAdminRecord) {
        List<ExamineApproveRegAdminRecord> list = examineApproveRegAdminRecordService.list();
        ExcelUtil<ExamineApproveRegAdminRecord> util = new ExcelUtil<ExamineApproveRegAdminRecord>(ExamineApproveRegAdminRecord.class);
        return util.exportExcel(list, "审批流程记录的 审核人员处理记录");
    }

    /**
     * 获取审批流程记录的 审核人员处理记录详细信息
     */
    @ApiOperation("获取审批流程记录的 审核人员处理记录详细信息")
    @PreAuthorize("@ss.hasPermi('module:adminRecord:query')")
    @GetMapping("/{id}")
    public AjaxResult<ExamineApproveRegAdminRecord> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
		QueryWrapper<ExamineApproveRegAdminRecord> qw = new QueryWrapper<>();
			qw.eq("approve_record_id",id);
		TableDataInfo<ExamineApproveRegAdminRecord> td = PageUtils.buildDataInfo(examineApproveRegAdminRecordService.page(PageUtils.buildPage(),qw));
		List<ExamineApproveRegAdminRecord> list = td.getRows();
		ExamineApproveRegAdminRecord record = new ExamineApproveRegAdminRecord();
		if (list.size()>0){
			record = list.get(list.size()-1);
			if (record.getHandleSort()!=null){
				if (record.getHandleSort()==0){
					record.setStateName("初审");
				}
				if (record.getHandleSort()==1){
					record.setStateName("复审");
				}
				if (record.getHandleSort()==2){
					record.setStateName("终审");
				}
			}
			if (record.getState()!=null){
				if (record.getState()==0){
					record.setStateName(record.getStateName()+"中");
				}
				if (record.getState()==1){
					record.setStateName(record.getStateName()+"通过");
				}
				if (record.getState()==2){
					record.setStateName(record.getStateName()+"驳回");
				}
			}
			if (record.getUserId()!=null){
				WxUser wxUser = wxUserService.findById(record.getUserId());
				if (wxUser!=null){
					record.setUserName(wxUser.getNickName());
				}
			}
		}
        return AjaxResult.success(record);
    }

    /**
     * 新增审批流程记录的 审核人员处理记录
     */
    @ApiOperation("新增审批流程记录的 审核人员处理记录")
    @PreAuthorize("@ss.hasPermi('module:adminRecord:add')")
    @Log(title = "审批流程记录的 审核人员处理记录", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody ExamineApproveRegAdminRecord examineApproveRegAdminRecord) {
        return toAjax(examineApproveRegAdminRecordService.save(examineApproveRegAdminRecord) ? 1 : 0);
    }

    /**
     * 修改审批流程记录的 审核人员处理记录
     */
    @ApiOperation("修改审批流程记录的 审核人员处理记录")
    @PreAuthorize("@ss.hasPermi('module:adminRecord:edit')")
    @Log(title = "审批流程记录的 审核人员处理记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody ExamineApproveRegAdminRecord examineApproveRegAdminRecord) {
        return toAjax(examineApproveRegAdminRecordService.updateById(examineApproveRegAdminRecord) ? 1 : 0);
    }

    /**
     * 删除审批流程记录的 审核人员处理记录
     */
    @ApiOperation("删除审批流程记录的 审核人员处理记录")
    @PreAuthorize("@ss.hasPermi('module:adminRecord:remove')")
    @Log(title = "审批流程记录的 审核人员处理记录" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return toAjax(examineApproveRegAdminRecordService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
